using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Media;

namespace Silvery
{
    /// <summary>
    /// 扩展带有自身循环的图像类
    /// </summary>
    public class NImageD : DynamicObject, IImage
    {
        /// <summary>
        /// 构造
        /// </summary>
        public NImageD()
        {
            CreateImage(null);
        }
        /// <summary>
        /// 带参数的构造，可以从Uri里面的到图片
        /// </summary>
        /// <param name="uri"></param>
        public NImageD(Uri uri)
        {
            CreateImage(uri);
        }
        /// <summary>
        /// 从uri创建图片
        /// </summary>
        /// <param name="uri">目标的uri</param>
        private void CreateImage(Uri uri)
        {
            this.Children.Add(content);
            if (uri != null)
                LoadImage(uri);
        }
        /// <summary>
        /// 读取图片
        /// 用以在特定时机加载
        /// </summary>
        /// <param name="uri">目标的uri</param>
        public void LoadImage(Uri uri)
        {
            content.Source = new BitmapImage(uri);
        }
        public void LoadImage(Stream streamSource)
        {
            var bitmapimage = new BitmapImage();
            bitmapimage.SetSource(streamSource);
            content.Source = bitmapimage;
        }
        Image content = new Image();
        /// <summary>
        /// 图片图像
        /// </summary>
        public Image Content { get { return content; } }
        private Point _Offset = new Point();
        /// <summary>
        /// 图片偏移点
        /// </summary>
        public Point OffsetPoint 
        {
            get { return _Offset; }
            set
            {
                _Offset = value;
                Canvas.SetLeft(content, value.X);
                Canvas.SetTop(content, value.Y);
            }
        }
        /// <summary>
        /// 销毁
        /// </summary>
        public override void Dispose()
        {
            Content.Source = null;
            this.Children.Remove(content);
            base.Dispose();
        }
        /// <summary>
        /// 克隆一个图片
        /// </summary>
        /// <returns></returns>
        public IImage CloneImage()
        {
            var nimage =  new NImageD();
            nimage.content.Source = Content.Source;
            nimage.OffsetPoint = this.OffsetPoint;
            nimage.Position = base.Position;
            nimage.SelfLoopInterval = this.SelfLoopInterval;
            nimage.IsSelfLoopRun = this.IsSelfLoopRun;
            return nimage;
        }
        protected override void ChangePositionTo(Point position)
        {
            base.ChangePositionTo(position);
            (this.TransformGroup.Children[1] as ScaleTransform).CenterX = position.X;
            (this.TransformGroup.Children[1] as ScaleTransform).CenterY = position.Y;
            (this.TransformGroup.Children[2] as RotateTransform).CenterX = position.X;
            (this.TransformGroup.Children[2] as RotateTransform).CenterY = position.Y;
        }

    }
}
